package kang.zhoujin.util;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.HashMap;

public class KeyboardUtil {
	private Robot robot = null;	
	private HashMap<Character, ButtonGroup> keyBoardMap = null;

	private class ButtonGroup{
		public boolean isShift;
		public int button;
		public ButtonGroup(int button){
			this.isShift = false;
			this.button = button;
		}
		public ButtonGroup(int button,boolean isShift){
			this.isShift = isShift;
			this.button = button;
		}
	}

	public KeyboardUtil() throws AWTException{
		robot = new Robot();	
		keyBoardMap = new HashMap<Character, ButtonGroup>();
		initKeyBoardMap();
	}

	private void initKeyBoardMap(){				
		//0~9
		for(int i = 48; i <= 57; i++){
			char c = (char) i;
			keyBoardMap.put(c, new ButtonGroup(i));
		}
		//a~z
		for(int i = 97; i <= 122; i++){
			char c = (char) i;
			int button = i - 32;
			keyBoardMap.put(c, new ButtonGroup(button));
		}
		//A~Z
		for(int i = 65; i <= 90; i++){
			char c = (char) i;
			keyBoardMap.put(c, new ButtonGroup(i,true));
		}

		keyBoardMap.put('`', new ButtonGroup(KeyEvent.VK_BACK_QUOTE));
		keyBoardMap.put('~', new ButtonGroup(KeyEvent.VK_BACK_QUOTE,true));
		keyBoardMap.put('!', new ButtonGroup(KeyEvent.VK_1,true));
		keyBoardMap.put('@', new ButtonGroup(KeyEvent.VK_2,true));
		keyBoardMap.put('#', new ButtonGroup(KeyEvent.VK_3,true));
		keyBoardMap.put('$', new ButtonGroup(KeyEvent.VK_4,true));
		keyBoardMap.put('%', new ButtonGroup(KeyEvent.VK_5,true));
		keyBoardMap.put('^', new ButtonGroup(KeyEvent.VK_6,true));
		keyBoardMap.put('&', new ButtonGroup(KeyEvent.VK_7,true));
		keyBoardMap.put('*', new ButtonGroup(KeyEvent.VK_8,true));
		keyBoardMap.put('(', new ButtonGroup(KeyEvent.VK_9,true));
		keyBoardMap.put(')', new ButtonGroup(KeyEvent.VK_0,true));
		keyBoardMap.put('-', new ButtonGroup(KeyEvent.VK_MINUS));
		keyBoardMap.put('_', new ButtonGroup(KeyEvent.VK_MINUS,true));
		keyBoardMap.put('=', new ButtonGroup(KeyEvent.VK_EQUALS));
		keyBoardMap.put('+', new ButtonGroup(KeyEvent.VK_EQUALS,true));
		keyBoardMap.put('[', new ButtonGroup(KeyEvent.VK_OPEN_BRACKET));
		keyBoardMap.put('{', new ButtonGroup(KeyEvent.VK_OPEN_BRACKET,true));
		keyBoardMap.put(']', new ButtonGroup(KeyEvent.VK_CLOSE_BRACKET));
		keyBoardMap.put('}', new ButtonGroup(KeyEvent.VK_CLOSE_BRACKET,true));
		keyBoardMap.put('\\', new ButtonGroup(KeyEvent.VK_BACK_SLASH));
		keyBoardMap.put('|', new ButtonGroup(KeyEvent.VK_BACK_SLASH,true));
		keyBoardMap.put(';', new ButtonGroup(KeyEvent.VK_SEMICOLON));
		keyBoardMap.put(':', new ButtonGroup(KeyEvent.VK_SEMICOLON,true));
		keyBoardMap.put('\'', new ButtonGroup(KeyEvent.VK_QUOTE));
		keyBoardMap.put('"', new ButtonGroup(KeyEvent.VK_QUOTE,true));
		keyBoardMap.put(',', new ButtonGroup(KeyEvent.VK_COMMA));
		keyBoardMap.put('<', new ButtonGroup(KeyEvent.VK_COMMA,true));
		keyBoardMap.put('.', new ButtonGroup(KeyEvent.VK_PERIOD));
		keyBoardMap.put('>', new ButtonGroup(KeyEvent.VK_PERIOD,true));
		keyBoardMap.put('/', new ButtonGroup(KeyEvent.VK_SLASH));
		keyBoardMap.put('?', new ButtonGroup(KeyEvent.VK_SLASH,true));
		keyBoardMap.put(' ', new ButtonGroup(KeyEvent.VK_SPACE));
	}
	public void keyClick(int button){
		robot.keyPress(button);
		robot.keyRelease(button);
	}

	public void keyClick(char c){
		ButtonGroup buttonGroup = keyBoardMap.get(c);
		if(buttonGroup != null && buttonGroup.isShift == false){
			robot.keyPress(buttonGroup.button);
			robot.keyRelease(buttonGroup.button);
		}		
	}

	public void keyClick(int button,int times){
		for(int i = 0; i < times; i++){
			robot.keyPress(button);
			robot.keyRelease(button);
			try{
				Thread.sleep(100);
			}catch(Exception e){

			}
		}
	}

	public void keyClick(char c,int times){
		ButtonGroup buttonGroup = keyBoardMap.get(c);
		if(buttonGroup != null && buttonGroup.isShift == false){
			for(int i = 0; i < times; i++){
				robot.keyPress(buttonGroup.button);
				robot.keyRelease(buttonGroup.button);
			}
		}		
	}

	public void keyPress(int button){
		robot.keyPress(button);		
	}

	public void keyPress(char c){
		ButtonGroup buttonGroup = keyBoardMap.get(c);
		if(buttonGroup != null && buttonGroup.isShift == false){
			robot.keyPress(buttonGroup.button);
		}
	}

	public void keyRelease(int button){
		robot.keyRelease(button);		
	}

	public void keyRelease(char c){
		ButtonGroup buttonGroup = keyBoardMap.get(c);
		if(buttonGroup != null && buttonGroup.isShift == false){
			robot.keyRelease(buttonGroup.button);
		}
	}


	public void inputText(String text){
		for(int i = 0; i < text.length(); i++){
			char c = text.charAt(i);
			ButtonGroup buttonGroup = keyBoardMap.get(c);
			if(buttonGroup != null){
				if(buttonGroup.isShift == false){
					robot.keyPress(buttonGroup.button);
					robot.keyRelease(buttonGroup.button);
				}else{
					robot.keyPress(KeyEvent.VK_SHIFT);
					robot.keyPress(buttonGroup.button);
					robot.keyRelease(buttonGroup.button);
					robot.keyRelease(KeyEvent.VK_SHIFT);
				}
			}
		}

	}


}
